$.fn.gallery = function (columns) {
    var cols = columns || 4;
    var $gallery = $(this);

    $($gallery).addClass('gallery');
    var width = cols * 260 + 'px';

    $($gallery)
        .find('.selected')
        .hide();

    $($gallery)
        .find('.previous-image')
        .css('top', '250px');
    $($gallery)
        .find('.next-image')
        .css('top', '250px');


    $($gallery)
        .find('.gallery-list')
        .css('width', width)
        .on('click', '.image-container', function () {
            var $clickedDiv = $(this);
            var currentImgDataInfo = $($clickedDiv)
                .children()
                .last()
                .attr('data-info');

            var nextImgDataInfo = (parseInt(currentImgDataInfo) + 1) % 12;
            if (nextImgDataInfo === 0) {
                nextImgDataInfo = 12;
            }
            var previousImgDataInfo = (parseInt(currentImgDataInfo) - 1);
            if (previousImgDataInfo === 0) {
                previousImgDataInfo = 12;
            }

            var current = parseInt(currentImgDataInfo);
            var previous = parseInt(currentImgDataInfo) - 1;
            var next = parseInt(currentImgDataInfo) + 1;

            $($gallery)
                .find('.selected img')
                .first()
                .attr('src', 'imgs/' + previousImgDataInfo + '.jpg')
                .parent()
                .addClass('previous-image')
                .on('click', function () {
                    var $clickedImg = $(this);

                    current--;
                    previous--;
                    next--;

                    if (current === 0) {
                        current = 12;
                    }

                    if (previous === 0) {
                        previous = 12;
                    }

                    if (next === 0) {
                        next = 12;
                    }

                    $($gallery)
                        .find('.selected #current-image')
                        .attr('src', 'imgs/' + current + '.jpg');

                    $($gallery)
                        .find('.selected img')
                        .first()
                        .attr('src', 'imgs/' + previous + '.jpg');

                    $($gallery)
                        .find('.selected img')
                        .last()
                        .attr('src', 'imgs/' + next + '.jpg');
                });
            $($gallery)
                .find('.selected #current-image')
                .attr('src', 'imgs/' + currentImgDataInfo + '.jpg')
                .parent()
                .addClass('current-image')
                .on('click', function () {
                    $($gallery).find('.selected').hide();
                    $($gallery).find('.image-container').removeClass('blurred');
                    $($gallery).find('.selected').removeClass('disabled-background');
                });

            $($gallery)
                .find('.selected img')
                .last()
                .attr('src', 'imgs/' + nextImgDataInfo + '.jpg')
                .parent()
                .addClass('next-image')
                .on('click', function () {
                    var $clickedImg = $(this);

                    current++;
                    previous++;
                    next++;

                    if (current === 13) {
                        current = 1;
                    }

                    if (previous === 13) {
                        previous = 1;
                    }

                    if (next === 13) {
                        next = 1;
                    }

                    $($gallery)
                        .find('.selected #current-image')
                        .attr('src', 'imgs/' + current + '.jpg');

                    $($gallery)
                        .find('.selected img')
                        .first()
                        .attr('src', 'imgs/' + previous + '.jpg');

                    $($gallery)
                        .find('.selected img')
                        .last()
                        .attr('src', 'imgs/' + next + '.jpg');
                });

            $($gallery).find('.image-container').addClass('blurred');
            $($gallery).find('.selected').addClass('disabled-background')
                .css('padding-top', '170px');

            $($gallery).find('.selected').show();
        });
};